
// 提示框功能
function showToast(message, type = 'info') {
    const container = document.getElementById('toast-container');

    // 如果容器不存在，创建一个
    if (!container) {
        const newContainer = document.createElement('div');
        newContainer.id = 'toast-container';
        document.body.appendChild(newContainer);
    }

    // 检查是否已存在相同内容的提示
    const existingToasts = document.getElementsByClassName('toast');
    for (let toast of existingToasts) {
        if (toast.textContent === message) {
            return; // 如果存在相同内容的提示，则不创建新的
        }
    }

    const toast = document.createElement('div');
    toast.className = `toast ${type}`;

    // 根据类型选择图标
    let icon = '';
    switch (type) {
        case 'success':
            icon = '<i class="fas fa-check-circle"></i>';
            break;
        case 'error':
            icon = '<i class="fas fa-times-circle"></i>';
            break;
        default:
            icon = '<i class="fas fa-info-circle"></i>';
    }

    toast.innerHTML = `${icon}${message}`;
    document.getElementById('toast-container').appendChild(toast);

    // 5秒后移除
    setTimeout(() => {
        if (document.getElementById('toast-container').contains(toast)) {
            toast.style.animation = 'fadeOut 0.3s ease forwards';
            toast.addEventListener('animationend', (e) => {
                if (e.animationName === 'fadeOut' &&
                    document.getElementById('toast-container') &&
                    document.getElementById('toast-container').contains(toast)) {
                    document.getElementById('toast-container').removeChild(toast);
                }
            });
        }
    }, 3000);
}

// 导出模块
module.exports = {
    showToast
};